package org.testmyapp.entities;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.testmyapp.persistence.BusinessObjectMarker;

@Entity
@IdClass(BillPositionPk.class)
public class BillPosition extends AbsProcessRelatedObject implements
		BusinessObjectMarker {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6088689031641459475L;

	public static String tableName = "BillPosition";

	@Temporal(value = TemporalType.DATE)
	private Date beginDate;
	@Temporal(value = TemporalType.DATE)
	private Date endDate;
	private Integer sum;
	@JoinColumn(name = "claim_fk", nullable = true)
	private Claim claim;
	@Column(nullable = true)
	private String otherClaim;
	@Id
	@ManyToOne(cascade = CascadeType.ALL)
	@JoinTable(name = "bill_id_fk", joinColumns = {
			@JoinColumn(name = "accountMedical_id", referencedColumnName = "accountingMedical_fk"),
			@JoinColumn(name = "bill_nr", referencedColumnName = "billNr") })
	private Bill bill;
	@Id
	private Integer billPositionNr;

	public Date getBeginDate() {
		return beginDate;
	}

	public void setBeginDate(Date beginDate) {
		this.beginDate = beginDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public Integer getSum() {
		return sum;
	}

	public void setSum(Integer sum) {
		this.sum = sum;
	}

	public Bill getBill() {
		return bill;
	}

	public void setBill(Bill bill) {
		this.bill = bill;
	}

	public Claim getClaim() {
		return claim;
	}

	public void setClaim(Claim claim) {
		this.claim = claim;
	}

	public String getOtherClaim() {
		return otherClaim;
	}

	public void setOtherClaim(String otherClaim) {
		this.otherClaim = otherClaim;
	}

	public Integer getBillPositionNr() {
		return billPositionNr;
	}

	public void setBillPositionNr(Integer billPositionNr) {
		this.billPositionNr = billPositionNr;
	}

	@Override
	public String toString() {
		String result = "Rechnung: " + bill + ", Position: " + billPositionNr
				+ bill.getBillNr() + ", start: " + beginDate + ", ende: "
				+ endDate + ", leistung: ";
		if (claim == null || claim.equals("")) {
			result += getOtherClaim();
		} else {
			result += getClaim();
		}
		result += ", betrag: " + getSum();
		return result;
	}

	public boolean equals(Object o) {
		if (o == this)
			return true;
		if (!(o instanceof BillPosition) || o == null)
			return false;
		BillPosition other = (BillPosition) o;
		if ((this.getBeginDate() == null && other.getBeginDate() != null)
				|| (!this.getBeginDate().equals(other.getBeginDate()))) {
			return false;
		}
		if ((this.getSum() == null && other.getSum() != null)
				|| !this.getSum().equals(other.getSum())) {
			return false;
		}
		if ((this.getBill() == null && other.getBill() != null)
				|| !this.getBill().equals(other.getBill())) {
			return false;
		}
		if ((this.getBillPositionNr() == null && other.getBillPositionNr() != null)
				|| !this.getBillPositionNr().equals(other.getBillPositionNr())) {
			return false;
		}
		if ((this.getEndDate() == null && other.getEndDate() != null)
				|| !this.getEndDate().equals(other.getEndDate())) {
			return false;
		}
		if (this.getClaim() == null) {
			if(other.getClaim() != null) {
				return false;
			}
		}
		else {
			if(!this.getClaim().equals(other.getClaim())) {
				return false;
			}
		}
		if (this.getOtherClaim() == null) {
			if(other.getOtherClaim() != null) {
				return false;
			}
		}
		else {
			if(!this.getOtherClaim().equals(other.getOtherClaim())) {
				return false;
			}
		}
		return true;
	}

}
